﻿// UniversalQuery.net,Universal Query control for .net WinFrom developers.
// For more infomation,please visit http://www.laiyunqing.cn
// Copyright (C) 2008 laiyunqing@gmail.com. All rights reserved.
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using cn.Laiyunqing.WinControls.Common;

namespace cn.Laiyunqing.WinControls
{
    //[ToolboxItem("查询控件")]
    /// <summary>
    /// 
    /// </summary>
    [ToolboxBitmap(typeof(UniversalQuery))]
    public partial class UniversalQuery : Component, ISupportInitialize
    {
        private QueryStringBuilder queryStructure = null;
        private bool enabled = true;
        private QueryWindowModel windowModel = QueryWindowModel.模式窗体;
        private string queryString = string.Empty;

        private List<QueryItem> queryItems = new List<QueryItem>();
        private QueryDbType dbType = QueryDbType.SqlServer;
        /// <summary>
        /// Occurs when [after do query clicked].
        /// </summary>
        [Browsable(true)]
        [Category("查询相关")]
        [Description("点击查询后触发的事件")]
        public event DoQueryEventHandler AfterDoQueryClicked;

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="UniversalQuery"/> is enabled.
        /// </summary>
        /// <value><c>true</c> if enabled; otherwise, <c>false</c>.</value>
        [Browsable(true)]
        [Bindable(true)]
        [Category("行为")]
        public bool Enabled
        {
            get
            {
                return this.enabled;
            }
            set
            {
                this.enabled = value;
            }
        }
        /// <summary>
        /// Gets or sets the window model.
        /// </summary>
        /// <value>The window model.</value>
        [Browsable(true)]
        [Bindable(true)]
        public QueryWindowModel WindowModel
        {
            get
            {
                return this.windowModel;
            }
            set
            {
                this.windowModel = value;
            }
        }
        /// <summary>
        /// Gets or sets the type of the db.
        /// </summary>
        /// <value>The type of the db.</value>
        [Browsable(true)]
        [Bindable(true)]
        public QueryDbType DbType
        {
            get
            {
                return this.dbType;
            }
            set
            {
                this.dbType = value;
            }
        }

        /// <summary>
        /// Gets or sets the items.
        /// </summary>
        /// <value>The items.</value>
        [Browsable(true)]
        [Bindable(true)]
        public List<QueryItem> Items
        {
            get
            {
                return this.queryItems;
            }
            set
            {
                this.queryItems = value;
            }
        }

        /// <summary>
        /// Gets or sets the query string.
        /// </summary>
        /// <value>The query string.</value>
        [Browsable(false)]
        [Bindable(false)]
        public string QueryString
        {
            get
            {
                return queryString;
            }
            set
            {
                this.queryString = value;
            }
        }
        private string m_Caption = "搜索助理";
        /// <summary>
        /// Gets or sets the caption.
        /// </summary>
        /// <value>The caption.</value>
        [Browsable(true)]
        [Bindable(true)]
        [Description("显示于查询窗口标题的文字")]
        public string Caption
        {
            get
            {
                return m_Caption;
            }
            set
            {
                m_Caption = value;
            }
        }

        private bool m_EnablePreView = true;
        /// <summary>
        /// Gets or sets a value indicating whether [enable SQL preview].
        /// </summary>
        /// <value><c>true</c> if [enable SQL preview]; otherwise, <c>false</c>.</value>
        [Browsable(true)]
        [Bindable(true)]
        public bool EnableSqlPreview
        {
            get
            {
                return m_EnablePreView;
            }
            set
            {
                m_EnablePreView = value;
            }
        }


        /// <summary>
        /// Fires the do query event.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The <see cref="cn.Laiyunqing.WinControls.DoQueryEventArgs"/> instance containing the event data.</param>
        public void FireDoQueryEvent(object sender, DoQueryEventArgs args)
        {
            if (AfterDoQueryClicked != null)
            {
                AfterDoQueryClicked(sender, args);
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="UniversalQuery"/> class.
        /// </summary>
        public UniversalQuery()
        {
        }

        /// <summary>
        /// Shows the dialog.
        /// </summary>
        public void ShowDialog()
        {
            if (this.Items.Count < 1)
            {
                MessageBox.Show("查询控件至少需包含一个字段定义。","系统信息",MessageBoxButtons.OK );
                return;
            }
            if (this.queryStructure == null || this.queryStructure.IsDisposed)
            {
                this.queryStructure = new QueryStringBuilder(this);
            }
            if (this.windowModel == QueryWindowModel.模式窗体)
                this.queryStructure.ShowDialog();
            else
                this.queryStructure.Show();
        }

        #region ISupportInitialize 成员

        /// <summary>
        /// 用信号通知对象初始化即将开始。
        /// </summary>
        public void BeginInit()
        {
            //throw new Exception("The method or operation is not implemented.");
            //this.queryStructure = new QueryStructure(this);
        }

        /// <summary>
        /// 用信号通知对象初始化已完成。
        /// </summary>
        public void EndInit()
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    }

    /// <summary>
    /// 
    /// </summary>
    public enum QueryWindowModel
    {
        /// <summary>
        /// 
        /// </summary>
        普通窗体,
        /// <summary>
        /// 
        /// </summary>
        模式窗体
    }

    /// <summary>
    /// 
    /// </summary>
    public enum QueryDbType
    {
        /// <summary>
        /// 
        /// </summary>
        SqlServer,
        /// <summary>
        /// 
        /// </summary>
        Oracle,
        /// <summary>
        /// 
        /// </summary>
        OLEDB,
        /// <summary>
        /// 
        /// </summary>
        ODBC
    }


}